<?php
namespace app\model;

use League\MimeTypeDetection\ExtensionMimeTypeDetector;
use think\Model;

class Roles extends Model
{

    protected $name = 'roles';
    /**
     * 角色列表
     */
    public function  roleList($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $pageSize = $param['page_size'] ?? 15 ;
        $page = $param['page'] ?? 1;
        $total = self::count();
        $list = self::page($page,$pageSize)->select();
//        foreach ($list as $key=>&$val){
//            $val->update_time = date('Y-m-d H:i:s',intval($val->update_time));
//            $val->create_time = date('Y-m-d H:i:s',$val->cerate_time);
//        }
        $result = [
            'total'=>$total,
            'list'=>$list,
        ];
        return getSuccessResponse('查询成功',$result);
    }


    /**
     * 获取所有角色列表
     */
    public function  allRoleList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $list = self::select();
        $result = [
            'list'=>$list,
        ];
        return getSuccessResponse('查询成功',$result);
    }

    /**
     * 删除角色
     */
    public function  delRole($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $ids = $param['ids'] ?? '';
        if (empty($ids)){
            return getFailResponse('参数有误');
        }
        $idsArr = explode(',',$ids);
        foreach ($idsArr as $val){
            self::where(['id'=>$val])->delete();
            AdminRoles::where(['role_id'=>$val])->delete();
            RolePermissions::where(['role_id'=>$val])->delete();
        }
        return getSuccessResponse('删除成功');
    }

    /**
     * 添加编辑角色
     */
    public function  addEditRole($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $role_name = $param['role_name'] ?? '';
        $remark = $param['remark'] ?? '';
        $permissions = $param['permissions'] ?? '';
        $id = $param['id'] ?? '';
        if (empty($role_name) || empty($permissions)){
            return getFailResponse('参数有误');
        }

        $roleInfo = self::where(['role_name'=>$role_name])->find();
        $data =[
            'role_name'=>$role_name,
            'remark'=>$remark,
            'create_time'=>time(),
            'update_time'=>time(),
        ];
        if (empty($id)){
            if (!empty($roleInfo)){
                return getFailResponse('角色已存在');
            }
           $save =  self::create($data);
           $role_id  = $save->id;
        }else{
            if (!empty($roleInfo) && $roleInfo['id'] != $id){
                return getFailResponse('角色已存在');
            }
            unset($data['create_time']);
            self::where(['id'=>$id])->update($data);
            RolePermissions::where(['role_id'=>$id])->delete();
            $role_id  = $id;
        }
        $preIdArr = explode(',',$permissions);
        foreach ($preIdArr as $val){
            $rolePre = [
                'role_id'=>$role_id,
                'permission_id'=>$val,
                'create_time'=>time(),
                'update_time'=>time(),
            ];
            RolePermissions::create($rolePre);
        }
        return getSuccessResponse();
    }

    /**
     * 角色详情
     */
    public function  roleDetail($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $detail = self::where(['id'=>$id])->find();
        return getSuccessResponse('查询成功',$detail);
    }
}